﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.BLL;
using Com.CMS263.PO;
using Com.CMS263.Util;
using System.Collections;
using System.IO;

namespace Com.CMS263.Web.manage.ManageBaseArt
{
    public partial class AddBaseArt : System.Web.UI.Page
    {
        public int maxOrder = 0;
        public ExtAttributeBLL extbll = new ExtAttributeBLL();
        public IList<ExtAttribute> extList = null;
        public ArtTypeBLL atbll = new ArtTypeBLL();
        public int page = 1;
        public int pid;
        int menuId;
        public string menuTitle = "";
        Com.CMS263.PO.ArtType type = null;
        public IList<ArtType> typelist = new List<ArtType>();

        protected void Page_Load(object sender, EventArgs e)
        {


            //根据父类类型盘点是否出默认结果

            pid = int.Parse(Request["pid"]);
            menuId = int.Parse(Request["menuId"]);
            extList = extbll.GetExtAttributeByTypeId(pid);
            type = atbll.GetArtTypeById(menuId);
            page = string.IsNullOrEmpty(Request["page"]) ? 1 : int.Parse(Request["page"]);
            menuTitle = type.typeName;
            //this.lbTypeName.Text = type.typeName;
            this.hdfTypeId.Value = pid.ToString();
            this.hdfMenuId.Value = menuId.ToString();





        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ArtBaseBLL abbll = new ArtBaseBLL();
            //文件按天存储，没有目录就自己创建
            string strDay = DateTime.Now.ToString("yyyy-MM-dd");
            string savePath = Server.MapPath("/ArtUpFile/" + strDay);
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            string title = Request["title"];
            if (!string.IsNullOrEmpty(title))
            {
                if (chkStrong.Checked)
                {
                    title = "<b>" + title + "</b>";

                }
                if (this.chkRed.Checked)
                {
                    title = "<span style='color:red'>" + title + "</span>";
                }
            }
            else
            { StringUtil.Alert("请输入标题", "-1"); }
            string addDate = Request["addDate"];

            int typeId = int.Parse(this.hdfTypeId.Value);
            int menuId = int.Parse(this.hdfMenuId.Value);
            ArtBase ab = new ArtBase();
            ab.title = title;
            ab.addDate = DateTime.Parse(addDate);
            ab.clickCount = int.Parse(Request["clickCount"]);

            string ordernums = Request["OrderNum"];
            int ordernum = 1;
            try
            {
                ordernum = Convert.ToInt32(ordernums);
                if (ordernum <= 0)
                {
                    StringUtil.Alert("排序请输入正整数", "-1");
                }
            }
            catch
            {

                StringUtil.Alert("排序请输入正整数", "-1");
            }

            ab.isBest = chkBest.Checked == true ? 1 : 0;
            ab.isShow = 1;
            ab.orderNum = ordernum;
            ab.typeId = typeId;
            ab.menuId = menuId;
            IList<ExtAttribute> attrList = extbll.GetExtAttributeByTypeId(typeId);
            IList<ExtValue> valList = new List<ExtValue>();
            int i = 0;
            //循环属性列表得到属性值并天数值列表
            foreach (ExtAttribute item in attrList)
            {
                string val = "";
                int notNull = item.notNull;

                if (item.attrType == 4 || item.attrType == 5)
                {
                    HttpPostedFile hpf = Request.Files[item.attrName];
                    if (notNull == 1)
                    {
                        if (hpf.ContentLength < 5)
                        {
                            StringUtil.Alert(item.attrTxtName + "是必填的", "-1");
                            return;
                        }
                    }
                    if (hpf.ContentLength < 5)
                    {
                        val = "";
                    }
                    else
                    {
                        
                        val = strDay + "/" + UpLoadFileUtil.UpLoadFile(Request.Files[item.attrName], savePath, i.ToString(),item.attrValue);
                    }
                    i++;
                }
                else if (item.attrType == 2)
                {
                    val = Request[item.attrName].Replace("'", "’");
                }
                else if (item.attrType == 3)
                {
                    if (notNull == 1)
                    {
                        if (string.IsNullOrEmpty(Request[item.attrName]))
                        {
                            StringUtil.Alert(item.attrTxtName + "是必填的", "-1");
                            return;
                        }
                        else if (Request[item.attrName].Length >= 1000000)
                        {
                            StringUtil.Alert("内容不能多于1000000字", "-1");
                            return;
                        }
                    }
                    val = Request[item.attrName];
                }
                else
                {
                    if (notNull == 1)
                    {
                        if (string.IsNullOrEmpty(Request[item.attrName]))
                        {
                            StringUtil.Alert(item.attrTxtName + "是必填的", "-1");
                            return;
                        }
                        else if (Request[item.attrName].Length >= 1000000)
                        {
                            StringUtil.Alert("内容不能多于1000000字", "-1");
                            return;
                        }
                    }
                    val = Request[item.attrName];
                }
                ExtValue ev = new ExtValue();
                ev.attrId = item.id;
                ev.val = val;
                valList.Add(ev);

            }
            ab.valueList = valList;

            abbll.Add(ab);
            AdminLoginInfo ali = AdminLoginCookie.GetLoginInfo();
            ArtTypeBLL atbll = new ArtTypeBLL();
            OperationLogBLL olbll = new OperationLogBLL();
            OperationLog ol = new OperationLog();
            ol.AddDate = DateTime.Now;
            ol.Ip = StringUtil.GetIP();
            ol.ModuleName = atbll.GetArtTypeById(ab.menuId).typeName;
            ol.Contents = "【添加】" + ab.title;
            ol.UserName = ali.UserName;
            olbll.Add(ol);
            StringUtil.Alert("添加成功", "BaseArtList.aspx?pid=" + pid.ToString() + "&menuId=" + menuId.ToString());
        }
        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {

        }
        public int GetMax()
        {
            ArtBaseBLL abbll = new ArtBaseBLL();
            return abbll.GetMaxArtOrderNumByMenuId(Convert.ToInt32(hdfMenuId.Value)) + 1;
        }

    }
}
